#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>

struct slab_test {
    unsigned long index;
    unsigned long offset;
    char *name;
};

/* 创建slab 缓存结构 和 slab使用对象 */
struct kmem_cache *slab_cachep;
EXPORT_SYMBOL(slab_cachep);

struct slab_test *slab_object;
EXPORT_SYMBOL(slab_object);

static int __init slab_alloc_demo_init(void)
{
    printk("slab cache init \r\n");
        
    /* 创建slab缓存*/
    slab_cachep = kmem_cache_create("Lishixian slabinfo", 
                            sizeof(struct slab_test), 0,
			    SLAB_RECLAIM_ACCOUNT,
			    NULL);
    if (!slab_cachep)
    {
        printk("error, create slab_cache fail! \r\n");
        return -ENOMEM;
    }

    /* 分配对象内存 */
    slab_object = kmem_cache_alloc(slab_cachep, GFP_KERNEL);  

    if (slab_object) 
    {
        printk("create slab object success \r\n");
        slab_object->index = 0x98;
        printk("%s INDEX: %#lx\n", __func__, slab_object->index);
    }
    
    return 0;
}

static void __exit slab_alloc_demo_exit(void)
{
    printk("slab_alloc_demo_exit \r\n");
}


module_init(slab_alloc_demo_init);
module_exit(slab_alloc_demo_exit);
MODULE_LICENSE("GPL");

